package com.hsamgle.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;

/**
 * @feature: TODO
 * @packge: com.hsamgle.utils
 * @author: 黄鹤老板
 * @create_time: 2019/7/6 14:00
 * @company: 江南皮革厂
 */
public final class IpUtils {

    /**
     * @method: TODO 获取本地ip
     * @time :	2019/7/6 14:00
     * @author: 黄鹤老板
     * @return:
     */
    public static String getLocalIp ( ) throws UnknownHostException {
        InetAddress ia = InetAddress.getLocalHost();
        return ia.getHostAddress();
    }


    /**
     * @method: TODO 获取公共ip
     * @time :	2019/7/6 14:00
     * @author: 黄鹤老板
     * @return:
     */
    public static String getPublicIp ( ) {
        BufferedReader in = null;
        try {
            URL url = new URL("http://checkip.amazonaws.com");
            in = new BufferedReader(new InputStreamReader(
                    url.openStream()));
            return in.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if ( in != null ) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }

}
